#include <stdio.h>
#include <limits.h>
#include <ctype.h>

int howManyBits(int);

int main() {
    // printf("%u\n", UINT_MAX);
    // printf("%d\n", INT_MIN);
    // printf("%d\n", INT_MAX);

    // printf("%cadfaef\n", '\057');
    // printf("%d\n", INT_MIN-INT_MIN);
    // printf("%u\n", (unsigned) 1.2);
    // printf("%u", (unsigned)0 -1);
    howManyBits(0x7fffffff);
}

int howManyBits(int x) {
  // 二分思想。如果不全是0，基数添加，并返回那一段的数
  // 如果全是0，则为1111，否则0000
  int h1 = 0xffff0000&x;
  printf("%08x\n", h1);
  int allZ = (!h1)<<31>>31;
  printf("%08x\n", allZ);
  int r1 = ((allZ^16)&16)|(allZ&0);
  printf("%08x\n", r1);
  int m1 = ((allZ^h1)&h1)|(allZ&((0x0000ffff&x)<<16));
  printf("%08x\n", m1);
  printf("================\n");
  // 第二轮
  h1 = 0xff000000&m1;
  allZ = (!h1)<<31>>31;
  r1 += (((allZ^8)&8)|(allZ&0));
  m1 = ((allZ^h1)&h1)|(allZ&((0x00ff0000&x)<<8));
  printf("%08x\n", h1);
  printf("%08x\n", allZ);
  printf("%08x\n", r1);
  printf("%08x\n", m1);
  printf("================\n");
  // 第三轮
  h1 = 0xf0000000&m1;
  allZ = (!h1)<<31>>31;
  r1 += (((allZ^4)&4)|(allZ&0));
  m1 = ((allZ^h1)&h1)|(allZ&((0x0f000000&x)<<4));
  printf("%08x\n", h1);
  printf("%08x\n", allZ);
  printf("%08x\n", r1);
  printf("%08x\n", m1);
  printf("================\n");
  // 第四轮
  h1 = 0xc0000000&m1;
  allZ = (!h1)<<31>>31;
  r1 += (((allZ^2)&2)|(allZ&0));
  m1 = ((allZ^h1)&h1)|(allZ&((0x30000000&x)<<2));
  printf("%08x\n", h1);
  printf("%08x\n", allZ);
  printf("%08x\n", r1);
  printf("%08x\n", m1);
  printf("================\n");
  // 第五轮
  h1 = 0x80000000&m1;
  allZ = (!h1)<<31>>31;
  r1 += (((allZ^1)&1)|(allZ&0));
  m1 = ((allZ^h1)&h1)|(allZ&((0x40000000&x)<<1));
  printf("%08x\n", h1);
  printf("%08x\n", allZ);
  printf("%08x\n", r1);
  printf("%08x\n", m1);
  printf("================\n");
  // 第轮
  h1 = 0x80000000&m1;
  allZ = (!h1)<<31>>31;
  r1 += (((allZ^1)&1)|(allZ&0));
  printf("%08x\n", h1);
  printf("%08x\n", allZ);
  printf("%08x\n", r1);
  printf("%08x\n", m1);
  return r1+1;
}